package com.baosight.risk.service.xtzy.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baosight.risk.api.xtzy.IXtzyOrgService;
import com.baosight.risk.base.mapper.BaseMapper;
import com.baosight.risk.base.redis.BaseRedis;
import com.baosight.risk.base.service.BaseServiceImpl;
import com.baosight.risk.common.bean.BeanUtils;
import com.baosight.risk.mapper.xtzy.XtzyOrgMapper;
import com.baosight.risk.mapper.xtzy.XtzyUserMapper;
import com.baosight.risk.service.xtzy.entity.XtzyOrg;
import com.baosight.risk.service.xtzy.entity.XtzyUser;
import com.baosight.risk.service.xtzy.vo.XtzyOrgVO;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @author zzw
 *
 */
@Service
@Slf4j
public class XtzyOrgServiceImpl extends BaseServiceImpl<XtzyOrg> implements IXtzyOrgService {
    @Autowired
    private XtzyOrgMapper orgMapper;
    @Autowired
    private XtzyUserMapper userMapper;
    @Autowired
    private BaseRedis redis;
    
    @Override
    public BaseMapper getMapper() {
        return orgMapper;
    }

    @Override
	public XtzyOrg getByBusiOrgCode(String busiOrgCode) {
    	XtzyOrg org = new XtzyOrg();
    	org.setBusiOrgCode(busiOrgCode);
    	org = orgMapper.selectOne(org);
		return org;
	}

	/**
     * 查询全部组织机构
     *
     * @param company 组织机构
     * @return
     */
    @Override
    public List<XtzyOrgVO> selectAllOrg(XtzyOrg org) {
        return orgMapper.selectAllOrg(org);
    }

	@Override
	public int save(XtzyOrg org) {
		if(StringUtils.isBlank(org.getId())){
			org = (XtzyOrg) BeanUtils.initBean(org);
			String parentGuid = org.getParentGuid();
			if(StringUtils.isBlank(parentGuid) || "0".equals(parentGuid)){
				org.setParentGuid("0");
				org.setPathCode("/"+org.getBusiOrgCode());
				org.setPathName("/"+org.getBusiOrgName());
				org.setPathLevel(1);
			}else{
				XtzyOrg parent = this.get(parentGuid);
				org.setPathCode(parent.getPathCode()+"/"+org.getBusiOrgCode());
				org.setPathName(parent.getPathName()+"/"+org.getBusiOrgName());
				org.setPathLevel(parent.getPathLevel()+1);
			}
			return this.insert(org);
		}else{
			return this.update(org);
		}
	}

	@Override
	public int deleteSubs(String id) {
		// TODO Auto-generated method stub
		return orgMapper.deleteSubs(id);
	}

	@Override
	public XtzyOrg getOneByMangeCompCode(String manageCompCode, String busiOrgClassify) throws Exception{
    	XtzyOrg org = new XtzyOrg();
    	org.setBusiCompCode(manageCompCode);
    	org.setBusiOrgClassify(busiOrgClassify);
    	List<XtzyOrg> orgs = orgMapper.select(org);
    	if(orgs.size()>1){
    		throw new Exception("职能机构只能有一条");
    	}
		return orgs.get(0);
	}

	@Override
	public List<XtzyUser> getUserAndOrgCodeAndRoleId(String busiOrgCode, String roleId) {
        String redisKey = "auth:userInfo:" + busiOrgCode + ":" + roleId;
        //如果缓存中数据已经存在，查询缓存
        if (redis.isKey(redisKey)) {
            return (List<XtzyUser>) redis.getValue(redisKey);
        }
        List<XtzyUser> userList = userMapper.getUserByOrgAndRole(busiOrgCode, roleId);
        redis.setValue(redisKey, userList);
        return userList;
	}

}
